package com.woniuxy.principal.controller;

import com.woniuxy.commons.anno.HasRole;
import com.woniuxy.commons.entity.AllTypeEnroll;
import com.woniuxy.commons.entity.ResponseEntity;
import com.woniuxy.commons.entity.SchoolGradeScore;
import com.woniuxy.commons.entity.WeakSubject;
import com.woniuxy.principal.service.GradeScoreService;
import com.woniuxy.principal.service.SchoolLeaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author： 雷鹏
 * Date: 2020/7/24 15:54
 */
@RestController
@RequestMapping("/grade")
public class GradeScoreController {

    @Autowired
    private GradeScoreService gradeScoreService;

    @Autowired
    private RedisTemplate<String, Object> redisTemlate;

    @Autowired
    private SchoolLeaderService schoolLeaderService;

    @HasRole("principal")
    @GetMapping("/TestType/{grade}")
    public ResponseEntity<List<String>> getTestType(@PathVariable("grade") String grade) {
        List<String> getTestType = gradeScoreService.getGetTestType(grade);

        ResponseEntity<List<String>> responseEntity = new ResponseEntity<>();
        responseEntity.setCode(200);
        responseEntity.setMsg("成功");
        responseEntity.setData(getTestType);

        return responseEntity;
    }

    @HasRole("principal")
    @GetMapping("/gradeScore/{grade}/{TestType}")
    public ResponseEntity<SchoolGradeScore> getGradeScore(@PathVariable("grade") String grade, @PathVariable("TestType") String TestType) {

        System.out.println(grade + "============" + TestType);
        SchoolGradeScore gradeScore = gradeScoreService.getGradeTotalScore(grade, TestType);

        ResponseEntity<SchoolGradeScore> responseEntity = new ResponseEntity<>();
        responseEntity.setCode(200);
        responseEntity.setMsg("成功");
        responseEntity.setData(gradeScore);

        System.out.println(responseEntity);
        return responseEntity;
    }

    @HasRole("principal")
    @GetMapping("/allGradeScore/{grade}/{TestType}")
    public ResponseEntity<String[][]> getAllGradeScore(@PathVariable("grade") String grade, @PathVariable("TestType") String TestType) {

        System.out.println(grade + "============" + TestType);
        String[][] allGradeScore = gradeScoreService.getAllGradeScoreCount(grade, TestType);

        ResponseEntity<String[][]> responseEntity = new ResponseEntity<>();
        responseEntity.setCode(200);
        responseEntity.setMsg("成功");
        responseEntity.setData(allGradeScore);

        System.out.println(responseEntity);
        return responseEntity;

    }

    @HasRole("principal")
    @GetMapping("/getAllSubjectScoreAvg/{grade}/{TestType}")
    public ResponseEntity<WeakSubject> getAllSubjectScoreAvg(HttpServletRequest request, @PathVariable("grade") String grade, @PathVariable("TestType") String TestType) {

        String refreshToken = request.getHeader("refreshToken");
        Map<Object, Object> data = redisTemlate.opsForHash().entries(refreshToken);
        int rid = (int) data.get("rid");
        //从数据库查出登陆账号所在学校id
        int sid = schoolLeaderService.getSid(rid);
        WeakSubject allSubjectScoreAvg = gradeScoreService.getAllSubjectScoreAvg(grade, TestType, sid);

        ResponseEntity<WeakSubject> responseEntity = new ResponseEntity<>();
        responseEntity.setCode(200);
        responseEntity.setMsg("成功");
        responseEntity.setData(allSubjectScoreAvg);

        return responseEntity;

    }


}
